#ifndef __SHM_HPP__
#define __SHM_HPP__

#include <iostream>
#include <string>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>

using namespace std;

const int gCreater = 1;
const int gUser = 2;
const string gpathname = "/home/lmy/25-04-09/Shm";
const int gproj_id = 0x66;
const int gShmSize = 4097; // 4096*n

class Shm
{
private:
    key_t GetCommKey()
    {
        key_t k = ftok(_pathname.c_str(), _proj_id);
        if (k < 0)
            perror("ftok");
        return k;
    }

    int GetShmHelper(key_t key, int size, int flag)
    {
        int shmid = shmget(key, size, flag);
        if (shmid < 0)
            perror("shmid");

        return shmid;
    }

    string RoleToString(int who)
    {
        if (who == gCreater)
            return "Creater";
        else if (who == gUser)
            return "User";
        else
            return "Done";
    }

    void* AttachShm()
    {
        if (_addrshm != nullptr)
            DetachShm(_addrshm);
        void* shmaddr = shmat(_shmid, nullptr, 0);
        if (shmaddr == nullptr)
            perror("shmat");
        cout << "who:" << RoleToString(_who) << "attach shm..." << endl;
        return shmaddr;
    }

    void DetachShm(void* shmaddr)
    {
        if (shmaddr == nullptr)
            return;
        shmdt(shmaddr);
        cout << "who:" << RoleToString(_who) << "detach shm..." << endl;
    }

public:
    Shm(const string pathname, int proj_id, int who)
        : _pathname(pathname), _proj_id(proj_id), _who(who), _addrshm(nullptr)
    {
        _key = GetCommKey();
        if (_who == gCreater)
            GetShmUserCreater();
        else if (_who == gUser)
            GetShmForUse();

        _addrshm = AttachShm();

        cout << "shmid" << _shmid << endl;
        cout << "_key" << ToHex(_key) << endl;
    }

    ~Shm()
    {
        if (_who == gCreater)
            int res = shmctl(_shmid, IPC_RMID, nullptr);
        cout << "shm remove done" << endl;
    }

    string ToHex(key_t key)
    {
        char buffer[128];
        snprintf(buffer, sizeof(buffer), "0x%x", key);
        return buffer;
    }

    bool GetShmUserCreater()
    {
        if (_who == gCreater)
        {
            _shmid = GetShmHelper(_key, gShmSize, IPC_CREAT | IPC_EXCL | 0666);
            if (_shmid >= 0)
                return true;
            cout << "shm create done..." << endl;
        }
        return false;
    }

    bool GetShmForUse()
    {
        if (_who == gUser)
        {
            _shmid = GetShmHelper(_key, gShmSize, IPC_CREAT | 0666);
            if (_shmid >= 0)
                return true;

            cout << "shm get done..." << endl;
        }
        return false;
    }

    void Zero()
    {
        if (_addrshm)
            memset(_addrshm, 0, gShmSize);
    }

    void* Addr()
    {
        return _addrshm;
    }

    void DebugShm()
    {
        struct shmid_ds ds;
        int n = shmctl(_shmid, IPC_STAT, &ds);
        if (n < 0)
            return;
        cout << "ds.shm_perm.__key : " << ToHex(ds.shm_perm.__key) << endl;
        cout << "ds.shm_nattch: " << ds.shm_nattch << endl;
    }

private:
    key_t _key;
    int _shmid;
    string _pathname;
    int _proj_id;
    int _who;
    void* _addrshm;
};

#endif